package com.example.hm_dtfkz.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.dq.ad.AdManager;
import com.dq.ad.UMManager;
import com.dq.ad.utils.ToastUtil;
import com.example.hm_dtfkz.BuildConfig;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.config.AppConfig;
import com.example.hm_dtfkz.config.LoginConfig;
import com.example.hm_dtfkz.databinding.DialogAnswerFailureBinding;
import com.example.hm_dtfkz.util.ActivityClickUtil;
import com.example.hm_dtfkz.util.AnimUtil;
import com.example.hm_dtfkz.util.AppUtils;
import com.example.hm_dtfkz.util.DQLog;
import com.example.hm_dtfkz.util.DisplayUtil;
import com.example.hm_dtfkz.util.Mmkv;
import com.example.hm_dtfkz.util.sound.MediaBackManager;
import com.kc.openset.ad.listener.OSETRewardListener;
import com.kc.openset.ad.reward.OSETRewardVideo;

/**
 * FileName: DialogAnswerFailure
 * Author: admin
 * Date: 2021/11/8 9:39
 * Description:
 */
public class DialogAnswerFailure extends Dialog implements View.OnClickListener {

    private DialogAnswerFailureBinding failureBinding;
    private final Activity activity;
    private OnClickLister onClickLister;
    private CountDown countDown;
    private Dialog  dialog;

    public DialogAnswerFailure(@NonNull Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        failureBinding = DialogAnswerFailureBinding.inflate(getLayoutInflater());
        setContentView(failureBinding.getRoot());
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);
        dialog=this;
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initData();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        String answerSum = "<font color='#FBFF41'>复活</font>后此次不扣除<font color='#FBFF41'>体力</font>";
        failureBinding.tvPrompt.setText(Html.fromHtml(answerSum));

        AnimUtil.receiveAnim(activity, failureBinding.imgResurrection);

        AdManager.getInstance().showNativeAD(activity, failureBinding.layoutBanner, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
            @Override
            public void onSuccess() {
                failureBinding.layoutBanner.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
            }

            @Override
            public void onClosed() {
                failureBinding.layoutBanner.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
            }

            @Override
            public void onError() {

            }

            @Override
            public void onClick() {

            }

            @Override
            public void onShow(View view) {

            }
        });

//        if (AdManager.isChannelHuawei()) {
//            failureBinding.tvCount.setVisibility(View.GONE);
//            failureBinding.tvNext.setVisibility(View.VISIBLE);
//        } else {
//            countDown = new CountDown(3000, 1000);
//            countDown.start();
//        }
    }

    private void initLister() {
        failureBinding.imgResurrection.setOnClickListener(this);
        failureBinding.btnClose.setOnClickListener(this);
    }

    class CountDown extends CountDownTimer {

        public CountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            DQLog.e("倒计时：" + millisUntilFinished);
            failureBinding.tvCount.setText(String.valueOf(millisUntilFinished / 1000 + 1));
        }

        @Override
        public void onFinish() {
            failureBinding.tvCount.setVisibility(View.GONE);
            failureBinding.btnClose.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        if (v.getId() == R.id.imgResurrection) {

            OSETRewardVideo.getInstance()
                    .setContext(activity)
                    .setUserId(LoginConfig.getUserOpenId())
                    .setPosId(AppUtils.getADID(1))
                    .showAd(activity, new OSETRewardListener() {
                        @Override
                        public void onClick() {

                        }

                        @Override
                        public void onClose(String s) {
                            MediaBackManager.getInstance(activity).start();
                        }

                        @Override
                        public void onReward(String s, int i) {
//                            ToastUtil.showShortToast(activity,i+"");
                            if (Mmkv.contains("cash_cow")) {
                                AppConfig.setCashCow(i+AppConfig.getCashCow());
                            }
                        }

                        @Override
                        public void onServiceResponse(int i) {

                        }

                        @Override
                        public void onShow(String s) {

                        }

                        @Override
                        public void onVideoEnd(String s) {
                            if (onClickLister != null) {
                                UMManager.onEvent(UMManager.Event.resurrection_click);
                                onClickLister.onResurrection(dialog);
                            }
                        }

                        @Override
                        public void onVideoStart() {
                            MediaBackManager.getInstance(activity)
                                    .pause();
                        }

                        @Override
                        public void onError(String s, String s1) {
                            ToastUtil.showShortToast(activity,s+"="+s1);
                        }
                    });
        } else if (v.getId() == R.id.btnClose) {
            if (this.onClickLister != null) {
                this.onClickLister.onNext(this);
            }
        }
    }

    public DialogAnswerFailure setOnClickLister(OnClickLister clickLister) {
        this.onClickLister = clickLister;
        return this;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (countDown != null) {
            countDown.cancel();
            countDown.onFinish();
            countDown = null;
        }
    }

    public interface OnClickLister {

        /**
         * 下一题
         *
         * @param dialog
         */
        void onNext(Dialog dialog);

        /**
         * 复活保持连对
         *
         * @param dialog
         */
        void onResurrection(Dialog dialog);
    }
}
